//
//  QuadrantFocusView.swift
//  ToDo4
//
//  Created by Jeyi Zhou on 2025/10/27.
//

import SwiftUI

struct QuadrantFocusView: View {
    let quadrant: Quadrant
    let title: String
    let color: Color
    let todos: [TodoItem]
    let onBack: () -> Void
    let onTodoToggle: (TodoItem) -> Void
    let onTodoDelete: (TodoItem) -> Void
    let onTodoEdit: (TodoItem) -> Void
    
    private var completedCount: Int {
        todos.filter { $0.isCompleted }.count
    }
    
    private var pendingCount: Int {
        todos.filter { !$0.isCompleted }.count
    }
    
    var body: some View {
        VStack(spacing: 0) {
            // 顶部导航栏
            HStack {
                Button(action: onBack) {
                    HStack(spacing: 8) {
                        Image(systemName: "chevron.left")
                            .font(.title2)
                        Text("返回")
                            .font(.headline)
                    }
                    .foregroundColor(color)
                }
                
                Spacer()
                
                VStack {
                    Text(title)
                        .font(.largeTitle)
                        .fontWeight(.bold)
                        .foregroundColor(color)
                    Text("\(todos.count) 个任务")
                        .font(.subheadline)
                        .foregroundColor(.secondary)
                }
                
                Spacer()
                
                // 添加任务按钮
                Button(action: {
                    // 这里需要传入添加任务的回调
                }) {
                    Image(systemName: "plus.circle.fill")
                        .font(.title2)
                        .foregroundColor(color)
                }
            }
            .padding()
            .background(Color(NSColor.windowBackgroundColor))
            
            // 统计信息
            HStack(spacing: 20) {
                StatCard(
                    title: "总任务",
                    value: "\(todos.count)",
                    color: color
                )
                
                StatCard(
                    title: "已完成",
                    value: "\(completedCount)",
                    color: .green
                )
                
                StatCard(
                    title: "待完成",
                    value: "\(pendingCount)",
                    color: .orange
                )
            }
            .padding(.horizontal)
            .padding(.vertical, 12)
            
            // 任务列表
            ScrollView {
                LazyVStack(spacing: 12) {
                    if todos.isEmpty {
                        VStack(spacing: 16) {
                            Image(systemName: "tray")
                                .font(.system(size: 48))
                                .foregroundColor(.secondary)
                            Text("暂无任务")
                                .font(.title2)
                                .foregroundColor(.secondary)
                            Text("点击右上角的 + 按钮添加新任务")
                                .font(.caption)
                                .foregroundColor(.secondary)
                        }
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                        .padding(.vertical, 60)
                    } else {
                        ForEach(todos) { todo in
                            FocusTodoRowView(
                                todo: todo,
                                onToggle: { onTodoToggle(todo) },
                                onDelete: { onTodoDelete(todo) },
                                onEdit: { onTodoEdit(todo) }
                            )
                        }
                    }
                }
                .padding()
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color(NSColor.windowBackgroundColor))
    }
}

struct StatCard: View {
    let title: String
    let value: String
    let color: Color
    
    var body: some View {
        VStack(spacing: 8) {
            Text(value)
                .font(.title)
                .fontWeight(.bold)
                .foregroundColor(color)
            Text(title)
                .font(.caption)
                .foregroundColor(.secondary)
        }
        .frame(maxWidth: .infinity)
        .padding(.vertical, 12)
        .background(
            RoundedRectangle(cornerRadius: 8)
                .fill(color.opacity(0.1))
                .overlay(
                    RoundedRectangle(cornerRadius: 8)
                        .stroke(color.opacity(0.3), lineWidth: 1)
                )
        )
    }
}

struct FocusTodoRowView: View {
    let todo: TodoItem
    let onToggle: () -> Void
    let onDelete: () -> Void
    let onEdit: () -> Void
    
    var body: some View {
        HStack(alignment: .top, spacing: 16) {
            // 完成状态按钮
            Button(action: onToggle) {
                Image(systemName: todo.isCompleted ? "checkmark.circle.fill" : "circle")
                    .foregroundColor(todo.isCompleted ? .green : .gray)
                    .font(.title2)
            }
            .buttonStyle(PlainButtonStyle())
            
            // 任务内容
            VStack(alignment: .leading, spacing: 8) {
                Text(todo.title)
                    .font(.title3)
                    .fontWeight(.medium)
                    .strikethrough(todo.isCompleted)
                    .foregroundColor(todo.isCompleted ? .secondary : .primary)
                
                if !todo.description.isEmpty {
                    Text(todo.description)
                        .font(.body)
                        .foregroundColor(todo.isCompleted ? .secondary : .primary)
                        .lineLimit(3)
                        .strikethrough(todo.isCompleted)
                }
                
                // 时间信息
                HStack {
                    Text("创建于 \(formatDate(todo.createdAt))")
                        .font(.caption)
                        .foregroundColor(.secondary)
                    
                    if todo.updatedAt != todo.createdAt {
                        Text("· 更新于 \(formatDate(todo.updatedAt))")
                            .font(.caption)
                            .foregroundColor(.secondary)
                    }
                }
            }
            
            Spacer()
            
            // 操作按钮
            VStack(spacing: 8) {
                Button(action: onEdit) {
                    Image(systemName: "pencil")
                        .foregroundColor(.blue)
                        .font(.callout)
                }
                .buttonStyle(PlainButtonStyle())
                
                Button(action: onDelete) {
                    Image(systemName: "trash")
                        .foregroundColor(.red)
                        .font(.callout)
                }
                .buttonStyle(PlainButtonStyle())
                .opacity(todo.isCompleted ? 1.0 : 0.7)
            }
        }
        .padding(16)
        .background(
            RoundedRectangle(cornerRadius: 12)
                .fill(Color.gray.opacity(0.1))
                .overlay(
                    RoundedRectangle(cornerRadius: 12)
                        .stroke(Color.gray.opacity(0.2), lineWidth: 1)
                )
        )
        .onTapGesture(count: 2) {
            onEdit()
        }
    }
    
    private func formatDate(_ date: Date) -> String {
        let formatter = DateFormatter()
        formatter.dateStyle = .short
        formatter.timeStyle = .short
        return formatter.string(from: date)
    }
}

#Preview {
    QuadrantFocusView(
        quadrant: .urgentImportant,
        title: "紧急重要",
        color: .red,
        todos: [
            TodoItem(title: "完成项目报告", description: "需要在今天下午5点之前提交给经理", quadrant: .urgentImportant),
            TodoItem(title: "客户会议", description: "下午3点的重要客户讨论", quadrant: .urgentImportant),
            TodoItem(title: "修复生产环境bug", description: "紧急修复影响用户的系统问题", quadrant: .urgentImportant)
        ],
        onBack: {},
        onTodoToggle: { _ in },
        onTodoDelete: { _ in },
        onTodoEdit: { _ in }
    )
    .frame(width: 800, height: 600)
}
